package com.twu.biblioteca.domain;

import com.twu.biblioteca.model.Borrowable;

public class LibraryService {

    private Library library;


    public LibraryService(Library library) {
        this.library = library;
    }

    public Borrowable checkOutBook(String bookId) {
        Borrowable book = library.findBook(bookId);
        if (book != null) {
            book.setBorrowerID(library.getLoginInCustomer().getUserId());
            library.moveBookToBorrowed(book);
        }
        return book;
    }

    public Borrowable checkOutMovie(String movieId) {
        Borrowable movie = library.findMovie(movieId);
        if (movie != null) {
            movie.setBorrowerID(library.getLoginInCustomer().getUserId());
            library.moveMovieToBorrowed(movie);
        }
        return movie;
    }


    public Borrowable returnBook(String bookId) {
        Borrowable book = library.findBorrowedBook(bookId);
        if (book != null) {
            book.setBorrowerID(null);
            library.moveBookToBorrowable(book);
        }

        return book;
    }

    public Borrowable returnMovie(String movieId) {
        Borrowable movie = library.findBorrowedMovie(movieId);
        if (movie != null) {
            movie.setBorrowerID(null);
            library.moveMovieToBorrowable(movie);
        }
        return movie;
    }
}
